package com.xiaoyu.lanling.widget.textview

import `in`.srain.cube.util.CheckUtil
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.InputFilter
import android.text.TextUtils
import android.util.AttributeSet
import androidx.emoji.widget.EmojiTextViewHelper
import com.qmuiteam.qmui.widget.textview.QMUILinkTextView
import com.xiaoyu.base.app.App
import com.xiaoyu.lanling.router.deeplink.DeepLinkRouter

class LinkEmojiTextView : QMUILinkTextView {

    private val sOnLinkClickListener: OnLinkClickListener = object : OnLinkClickListener {
        override fun onTelLinkClick(phoneNumber: String) {
            if (!enableClickForLink || TextUtils.isEmpty(phoneNumber)) {
                return
            }
            val uri = Uri.parse("tel:$phoneNumber")
            val intent = Intent(Intent.ACTION_DIAL, uri)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

            if (CheckUtil.checkAppsIsExist(context, intent)) {
                App.getInstance().topActivity?.startActivity(intent)
            }
        }

        override fun onMailLinkClick(mailAddress: String) {
            if (!enableClickForLink || TextUtils.isEmpty(mailAddress)) {
                return
            }
            val uri = Uri.parse("mailto:$mailAddress")
            val intent = Intent(Intent.ACTION_SENDTO, uri)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            if (CheckUtil.checkAppsIsExist(context, intent)) {
                App.getInstance().topActivity?.startActivity(intent)
            }
        }

        override fun onWebUrlLinkClick(url: String) {
            if (!enableClickForLink || TextUtils.isEmpty(url)) {
                return
            }
            DeepLinkRouter.getInstance().dispatch(context, url)
        }
    }

    private var mEmojiTextViewHelper: EmojiTextViewHelper? = null
    private var mInitialized = false
    var enableClickForLink: Boolean = false

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    override fun onDetachedFromWindow() {
        setOnLinkClickListener(null)
        super.onDetachedFromWindow()
    }

    override fun onAttachedToWindow() {
        setOnLinkClickListener(sOnLinkClickListener)
        super.onAttachedToWindow()
    }

    private fun init() {
        if (!mInitialized) {
            mInitialized = true
            emojiTextViewHelper?.updateTransformationMethod()
        }
    }

    override fun setFilters(filters: Array<InputFilter>) {
        super.setFilters(emojiTextViewHelper?.getFilters(filters))
    }

    override fun setAllCaps(allCaps: Boolean) {
        super.setAllCaps(allCaps)
        emojiTextViewHelper?.setAllCaps(allCaps)
    }

    private val emojiTextViewHelper: EmojiTextViewHelper?
        get() {
            if (mEmojiTextViewHelper == null) {
                mEmojiTextViewHelper = EmojiTextViewHelper(this)
            }
            return mEmojiTextViewHelper
        }
}